package com.rb.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rb.dao.Auth_codeDAO;
import com.rb.dao.RobotDAO;
import com.rb.dao.Ru_relationsDAO;
import com.rb.dao.Secret_keyDAO;
import com.rb.dao.UserDAO;
import com.rb.dao.User_logDAO;
import com.rb.model.Auth_code;
import com.rb.model.Robot;
import com.rb.model.Ru_relations;
import com.rb.model.Secret_key;
import com.rb.model.User;
import com.rb.model.User_log;
import com.rb.util.SetSecretKey;

/**
 * @author 作者 :ZYY
 * @version 创建时间：2016年12月16日 上午11:23:33 类说明
 */
@Service
public class UserServiceDAOImpl implements UserServiceDAO {
	@Autowired
	private RobotDAO robotDAO;

	@Autowired
	private UserDAO userDAO;

	@Autowired
	private User_logDAO user_logDAO;

	@Autowired
	private Ru_relationsDAO ru_relationsDAO;

	@Autowired
	private Secret_keyDAO secret_keyDAO;

	@Autowired
	private Auth_codeDAO auth_codeDAO;

	// 注册机器人信息
	@Override
	public Map<String, Object> addRobot(Robot robot) {
		Map<String, Object> map = new HashMap<>();

		int n = 0;
		// 先判断是否已注册
		Robot robot2 = judgeRobot(robot.getUid());
		if (robot2 == null) {
			n = robotDAO.insert(robot);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "机器人注册成功！");
			} else {
				map.put("flag", false);
				map.put("message", "后台出错");
			}

		} else {
			map.put("flag", false);
			map.put("message", "机器人已注册！");
		}
		return map;
	}

	// 根据uid查询机器人信息
	@Override
	public Robot judgeRobot(String uid) {
		Robot robot = robotDAO.showRobotByUid(uid);
		return robot;
	}

	// 用户注册
	@Override
	public Map<String, Object> addUser(User user, String uid, String auth_code) {
		Map<String, Object> map = new HashMap<>();
		// 判断用户是否已注册
		User user2 = findUserByAccount(user.getAccount());
		// 判断当前机器人的主人的状态，没有则定当前用户为主人
		Robot robot = robotDAO.showRobotByUid(uid);
		// 验证码
		Auth_code auth_code2 = findAuthCodeByAccount(user.getAccount());

		boolean b = auth_code.equals(auth_code2.getAuth_code());
		if (user2 != null) {
			map.put("message", "该账号已经注册！");
			map.put("flag", false);
			return map;
		} else if (!b) {
			map.put("message", "验证码错误！");
			map.put("flag", false);
			return map;
		} else if ("0".equals(robot.getMaster_account())) {
			if (user2 == null) {
				int n = userDAO.insert(user);
				robot.setMaster_account(user.getAccount());
				n += robotDAO.updateByPrimaryKey(robot);
				Ru_relations relations = new Ru_relations(user.getAccount(), robot.getUid());
				n += ru_relationsDAO.insert(relations);
				if (n == 3) {
					map.put("flag", true);
					map.put("message", "用户注册成功！");
					return map;
				} else {
					map.put("flag", false);
					map.put("message", "后台出错");
					return map;
				}
			}
		}else {
			map.put("flag", false);
			map.put("message", "该用户或机器人已经注册！");
		}
		return map;
	}

	// 根据账号查询用户
	@Override
	public User findUserByAccount(String account) {
		User user = userDAO.findUserByAccount(account);
		return user;
	}

	// 修改机器人信息
	@Override
	public Map<String, Object> updateRobot(String uid, String newName, Timestamp time) {
		Map<String, Object> map = new HashMap<>();
		// 先判断机器人是否存在
		Robot robot2 = robotDAO.showRobotByUid(uid);
		if (robot2 != null) {
			robot2.setName(newName);
			robot2.setActivate(1);
			robot2.setActivate_time(time);
			int n = robotDAO.updateByPrimaryKey(robot2);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "机器人信息修改成功！");
			} else {
				map.put("flag", false);
				map.put("message", "后台出错！");
			}

		} else {
			map.put("flag", false);
			map.put("message", "机器人未注册！");
		}
		return map;
	}

	// 用户登录
	@Override
	public Map<String, Object> userLogin(String account, String password, Timestamp time) {
		Map<String, Object> map = new HashMap<>();

		// 判断用户是否存在
		User user = findUserByAccount(account);
		if (user == null) {
			map.put("flag", false);
			map.put("message", "该账号未注册！");
			return map;
		} else if (!user.getAccount().equals(account) || !user.getPassword().equals(password)) {
			map.put("flag", false);
			map.put("message", "账号或者密码错误！");
			return map;
		} else {
			boolean flag = false;
			// 修改用户状态
			user.setStatus(1);
			flag = changeUser(user);
			// 添加一条登录日志
			User_log user_log = new User_log(account, "登录", time);
			flag &= addLog(user_log);
			// 用户密钥操作
			if ("false".equals(changeSecretKey(account).get("flag").toString())) {// 不存在
				String string = SetSecretKey.generateString();
				Secret_key secret_key = new Secret_key(account, string);
				secret_keyDAO.insert(secret_key);
			} else {
				Secret_key secret_key = findSecretKeyByAccount(account);
				String string = SetSecretKey.generateString();
				secret_key.setSr_key(string);
				secret_keyDAO.updateByPrimaryKey(secret_key);
			}
			if (flag) {
				map.put("flag", flag);
				map.put("message", "用户登录成功！");
			}
		}

		return map;
	}

	// 添加日志
	@Override
	public boolean addLog(User_log user_log) {
		boolean flag = false;
		int n = user_logDAO.insert(user_log);
		flag = (n == 1 ? true : false);
		return flag;
	}

	// 修改用户状态
	@Override
	public boolean changeUser(User user) {
		boolean flag = false;
		int n = userDAO.updateByPrimaryKey(user);
		flag = (n == 1 ? true : false);
		return flag;
	}

	// 登出
	@Override
	public Map<String, Object> userLogout(String account, Timestamp time) {
		Map<String, Object> map = new HashMap<>();
		User user = userDAO.findUserByAccount(account);

		if (user == null) {
			map.put("flag", false);
			map.put("message", "该账号不存在！");
			return map;
		} else {
			boolean flag = false;
			user.setStatus(0);
			flag = changeUser(user);
			// 添加一条登录日志
			User_log user_log = new User_log(account, "退出", time);
			flag &= addLog(user_log);
			// 用户密钥操作
			Secret_key secret_key = findSecretKeyByAccount(account);
			secret_key.setSr_key("0");
			secret_keyDAO.updateByPrimaryKey(secret_key);
			if (flag) {
				map.put("flag", flag);
				map.put("message", "登出成功！");
			}
		}

		return map;
	}

	// 校验机器人注册情况
	@Override
	public Map<String, Object> judgeRobotStatus(String uid) {
		Map<String, Object> map = new HashMap<>();
		// 根据uid获得当前机器人信息
		Robot robot = robotDAO.showRobotByUid(uid);
		if (robot == null) {
			map.put("flag", false);
			map.put("message", "机器人未注册！");
			return map;
		} else if (robot.getMaster_account() == null || robot.getMaster_account() == "") {
			map.put("flag", false);
			map.put("message", "机器人未激活！");
			return map;
		} else {
			map.put("flag", true);
			map.put("message", "机器人已经注册！");
		}

		return map;
	}

	// 用户登录后，权限验证
	@Override
	public List<Object> judgeUserLogin(String account) {
		// 此用户操作是经过登录验证后，不必再验证是否注册
		// 根据账号查询能操作机器人uid(主人)
		List<Object> list = new ArrayList<>();
		Robot robot = robotDAO.showRobotByAccount(account);
		if (robot != null) {
			list.add(true);
			list.add(robot.getActivate());
			list.add(robot.getUid());
		} else {
			list.add(false);
			list.add(0);
			list.add(0);
		}
		return list;
	}

	// 普通用户注册(手机端)
	@Override
	public Map<String, Object> addUser1(User user, String auth_code) {
		Map<String, Object> map = new HashMap<>();

		// 判断用户是否已注册
		User user2 = findUserByAccount(user.getAccount());

		// 判断验证码是否正确
		Auth_code auth_code2 = findAuthCodeByAccount(user.getAccount());
		//
		if (user2 != null) {
			map.put("flag", false);
			map.put("message", "该账号已注册！");
			return map;
		} else if (!auth_code.equals(auth_code2.getAuth_code())) {
			map.put("flag", false);
			map.put("message", "验证码错误！");
			return map;
		} else {
			int n = userDAO.insert(user);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "用户注册成功！");
				return map;
			} else {
				map.put("flag", false);
				map.put("message", "后台出错！");
			}
		}

		return map;
	}

	// 根据账号获得用户密钥
	@Override
	public Secret_key findSecretKeyByAccount(String account) {
		Secret_key secret_key = secret_keyDAO.getSecretKeyByAccount(account);
		return secret_key;
	}

	@Override
	public Map<String, Object> changeSecretKey(String account) {
		Map<String, Object> map = new HashMap<>();
		// 先判断是否存在
		Secret_key secret_key2 = findSecretKeyByAccount(account);
		if (secret_key2 == null) {
			map.put("flag", false);
			map.put("message", "用户秘钥不存在！");
			return map;
		} else {
			map.put("flag", true);
			map.put("message", "用户秘钥存在！");
		}
		return map;

	}

	// 添加验证码
	@Override
	public Map<String, Object> addAuthCode(String account, String code, Timestamp time) {
		Map<String, Object> map = new HashMap<>();
		// 判断验证码是否存在
		Auth_code auth_code2 = auth_codeDAO.findAuthCodeByAccount(account);
		if (auth_code2 != null) {
			// 此处应该添加获取验证码的时间限制
			auth_code2.setAuth_code(code);
			int n = auth_codeDAO.updateByPrimaryKey(auth_code2);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "验证码更新成功！");
				return map;
			}else {
				map.put("flag", false);
				map.put("message", "验证码更新失败！");
				return map;
			}
			
		} else {
			auth_code2 = new Auth_code(account, code, time);
			int n = auth_codeDAO.insert(auth_code2);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "验证码添加成功！");
				return map;
			}else {
				map.put("flag", false);
				map.put("message", "验证码添加失败！");
			}
		}

		return map;
	}

	// 查询验证码
	@Override
	public Auth_code findAuthCodeByAccount(String account) {
		Auth_code auth_code = auth_codeDAO.findAuthCodeByAccount(account);
		return auth_code;
	}

	//修改用户昵称
	@Override
	public Map<String, Object> changeUserName(String mobile, String name) {
		Map<String, Object> map = new HashMap<>();
		User user = userDAO.findUserByAccount(mobile);
		if (user == null) {
			map.put("flag", true);
			map.put("message", "用户不存在！");
			return map;
		}else {
			user.setName(name);
			int n = userDAO.updateByPrimaryKey(user);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "用户昵称修改成功！");
				return map;
			}else {
				map.put("flag", false);
				map.put("message", "用户昵称修改失败！");
			}
		}
		return map;
	}
	//修改头像
	@Override
	public Map<String, Object> changeUserHead(String mobile, String head,String head_name) {
		Map<String, Object> map = new HashMap<>();
		User user = userDAO.findUserByAccount(mobile);
		if (user == null) {
			map.put("flag", true);
			map.put("message", "用户不存在！");
			return map;
		}else {
			user.setHead(head);
			user.setHead_name(head_name);
			int n = userDAO.updateByPrimaryKey(user);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "用户头像修改成功！");
				return map;
			}else {
				map.put("flag", false);
				map.put("message", "用户头像修改失败！");
			}
		}
		
		return map;
	}
	//修改密码
	@Override
	public Map<String, Object> changeUserPwd(String mobile, String password,String auth_code) {
		Map<String, Object> map = new HashMap<>();
		User user = userDAO.findUserByAccount(mobile);
		Auth_code auth_code2 = auth_codeDAO.findAuthCodeByAccount(mobile);
		if (user == null) {
			map.put("flag", false);
			map.put("message", "用户不存在！");
			return map;
		}else if (auth_code2 == null){
			map.put("flag", false);
			map.put("message", "验证码不存在！");
			return map;
		}else if (!auth_code.equals(auth_code2.getAuth_code().toString())) {
			map.put("flag", false);
			map.put("message", "验证码错误！");
			return map;
		}else {
			user.setPassword(password);
			int n = userDAO.updateByPrimaryKey(user);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "密码修改成功！");
				return map;
			}else {
				map.put("flag", false);
				map.put("message", "密码修改失败！");
			}
		}
		
		return map;
	}
	//修改用户手机号，原理就是删除原来的，重新添加一个用户
	@Override
	public Map<String, Object> changeUserMobile(String mobile, String newMobile,String auth_code,Timestamp time) {
		Map<String, Object> map = new HashMap<>();
		User user = userDAO.findUserByAccount(mobile);
		Auth_code auth_code2 = auth_codeDAO.findAuthCodeByAccount(mobile);
		if (user == null) {
			map.put("flag", false);
			map.put("message", "用户不存在！");
			return map;
		}else if (auth_code2 == null){
			map.put("flag", false);
			map.put("message", "验证码不存在！");
			return map;
		}else if (!auth_code.equals(auth_code2.getAuth_code().toString())) {
			map.put("flag", false);
			map.put("message", "验证码错误！");
			return map;
		}else {
			User user2 = new User();
			user2.setPassword(user.getPassword());
			user2.setAccount(newMobile);
			user2.setCreat_time(time);
			user2.setHead(user.getHead());
			user2.setHead_name(user.getHead_name());
			user2.setName(user.getName());
			user2.setRole(user.getRole());
			user2.setStatus(0);
			int n = userDAO.delete(user);
			n += userDAO.insert(user2);
			if (n == 2) {
				map.put("flag", true);
				map.put("message", "手机号修改成功！");
				return map;
			}else {
				map.put("flag", false);
				map.put("message", "手机号修改失败！");
			}
		}
		
		return map;
	}
	//根据uid获得机器人信息
	@Override
	public Robot findRobotByUid(String uid) {
		Robot robot = robotDAO.showRobotByUid(uid);
		return robot;
	}

	@Override
	public Robot findRobotByAccount(String account) {
		Robot robot = robotDAO.showRobotByAccount(account);
		return robot;
	}
	//修改密码
	@Override
	public Map<String, Object> changeUserPwd1(String mobile, String password, String newpassword) {
		Map<String, Object> map = new HashMap<>();
		User user = userDAO.findUserByAccount(mobile);
		if (user == null) {
			map.put("flag", false);
			map.put("message", "账号或密码不正确！");
			return map;
		}else {
			user.setPassword(newpassword);
			int n = userDAO.updateByPrimaryKey(user);
			if (n == 1) {
				map.put("flag", true);
				map.put("message", "密码修改成功！");
				return map;
			}else {
				map.put("flag", false);
				map.put("message", "密码修改失败！");
			}
		}
		return map;
	}

	@Override
	public int deleteUserByAccount(String account) {
		int n = 1;
		User user = userDAO.findUserByAccount(account);
		Robot robot = robotDAO.showRobotByAccount(account);
		Secret_key secret_key = secret_keyDAO.getSecretKeyByAccount(account);
		Ru_relations ru_relations = ru_relationsDAO.findRUByAccount(account);
		if (user != null) {
			n *= userDAO.delete(user);
		}
		if (robot != null) {
			n *= robotDAO.delete(robot);
		}
		if (secret_key != null) {
			n *= secret_keyDAO.delete(secret_key);
		}
		if (ru_relations != null) {
			n *= ru_relationsDAO.delete(ru_relations);
		}
		return n;
	}

	@Override
	public int deleteRobotByUid(String uid) {
		int n = 1;
		Robot robot = robotDAO.showRobotByUid(uid);
		if (robot != null) {
			User user = userDAO.findUserByAccount(robot.getMaster_account());
			Ru_relations relations = ru_relationsDAO.findRUByAccount(robot.getMaster_account());
			Secret_key secret_key = secret_keyDAO.getSecretKeyByAccount(robot.getMaster_account());
			n *= userDAO.delete(user);
			n *= ru_relationsDAO.delete(relations);
			n *= secret_keyDAO.delete(secret_key);
			n *= robotDAO.delete(robot);
		}
		return n;
	}

	@Override
	public List<Robot> showAllRobot() {
		List<Robot> list = robotDAO.selectAll();
		return list;
	}

	@Override
	public List<User> showAllUser() {
		List<User> list = userDAO.selectAll();
		return list;
	}

}
